<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=7.4.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.json',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="想在公网上实现视频通信，需要下面3个核心元素：  一个是NAT穿透服务器(ICE Server)，实现内网穿透。 基于WebSocket的信令服务器(Signaling Server)，用于建立点对点的通道。 Web客户端。通过H5的WebRTC特性调用摄像头，进行用户交互。">
<meta name="keywords" content="webrtc">
<meta property="og:type" content="article">
<meta property="og:title" content="搭建webRTC视频聊天">
<meta property="og:url" content="https://unix2dos.github.io/p/697b4dfa.html">
<meta property="og:site_name" content="Levon&#39;s Blog">
<meta property="og:description" content="想在公网上实现视频通信，需要下面3个核心元素：  一个是NAT穿透服务器(ICE Server)，实现内网穿透。 基于WebSocket的信令服务器(Signaling Server)，用于建立点对点的通道。 Web客户端。通过H5的WebRTC特性调用摄像头，进行用户交互。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://unix2dos.github.io/p/697b4dfa/1.png">
<meta property="og:updated_time" content="2019-09-27T15:36:51.108Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="搭建webRTC视频聊天">
<meta name="twitter:description" content="想在公网上实现视频通信，需要下面3个核心元素：  一个是NAT穿透服务器(ICE Server)，实现内网穿透。 基于WebSocket的信令服务器(Signaling Server)，用于建立点对点的通道。 Web客户端。通过H5的WebRTC特性调用摄像头，进行用户交互。">
<meta name="twitter:image" content="https://unix2dos.github.io/p/697b4dfa/1.png">
  <link rel="canonical" href="https://unix2dos.github.io/p/697b4dfa">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>搭建webRTC视频聊天 | Levon's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Levon's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">微信:  L6241425</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>时光</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://unix2dos.github.io/p/697b4dfa.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Levon">
      <meta itemprop="description" content="Never Give Up">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Levon's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">搭建webRTC视频聊天

          
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-01-29 16:00:00" itemprop="dateCreated datePublished" datetime="2019-01-29T16:00:00+08:00">2019-01-29</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-09-27 23:36:51" itemprop="dateModified" datetime="2019-09-27T23:36:51+08:00">2019-09-27</time>
              </span>
            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/1-编程语言/" itemprop="url" rel="index"><span itemprop="name">1-编程语言</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/1-编程语言/javascript/" itemprop="url" rel="index"><span itemprop="name">javascript</span></a></span>

                
                
              
            </span>
          

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
        
      
      <span class="post-meta-item-text">Disqus：</span>
    
    <a title="disqus" href="/p/697b4dfa.html#comments" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="p/697b4dfa.html" itemprop="commentCount"></span></a>
  </span>
  
  
          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
              
                <span class="post-meta-item-text">本文字数：</span>
              
              <span>5.4k</span>
            </span>
          
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
              
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              
              <span>5 分钟</span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>想在公网上实现视频通信，需要下面3个核心元素：</p>
<ol>
<li>一个是NAT穿透服务器(ICE Server)，实现内网穿透。</li>
<li>基于WebSocket的信令服务器(Signaling Server)，用于建立点对点的通道。</li>
<li>Web客户端。通过H5的WebRTC特性调用摄像头，进行用户交互。</li>
</ol>
<a id="more"></a>

<h2 id="1-搭建NAT穿透服务器coturn"><a href="#1-搭建NAT穿透服务器coturn" class="headerlink" title="1. 搭建NAT穿透服务器coturn"></a>1. 搭建NAT穿透服务器coturn</h2><ul>
<li>教程:</li>
</ul>
<p><a href="https://github.com/coturn/coturn/wiki/README" target="_blank" rel="noopener">https://github.com/coturn/coturn/wiki/README</a></p>
<ul>
<li>准备工作: </li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">sudo yum install gcc</span><br><span class="line">sudo yum install openssl-devel</span><br><span class="line">sudo yum install libevent</span><br><span class="line">sudo yum install libevent-devel</span><br><span class="line">sudo yum install sqlite</span><br><span class="line">sudo yum install sqlite-devel</span><br><span class="line"></span><br><span class="line">参见: https://github.com/coturn/coturn/blob/master/INSTALL</span><br></pre></td></tr></table></figure>

<ul>
<li>开始安装:</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git clone https://github.com/coturn/coturn</span><br><span class="line">./configure</span><br><span class="line">make</span><br><span class="line">sudo make install</span><br></pre></td></tr></table></figure>

<ul>
<li>启动:</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">cp examples/etc/turnserver.conf bin/turnserver.conf</span><br><span class="line">vi bin/turnserver.conf</span><br><span class="line"></span><br><span class="line">修改配置turnserver.conf，如下：</span><br><span class="line"><span class="meta">#</span><span class="bash">监听端口 </span></span><br><span class="line">listening-port=3478 </span><br><span class="line"><span class="meta">#</span><span class="bash">内网IP </span></span><br><span class="line">listening-ip=你的服务器内网IP</span><br><span class="line"><span class="meta">#</span><span class="bash">外网IP地址 </span></span><br><span class="line">external-ip=你的服务器外网IP</span><br><span class="line"><span class="meta">#</span><span class="bash">访问的用户、密码 </span></span><br><span class="line">user=user:password</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">cd bin</span><br><span class="line">turnserver -v -r 207.246.80.69:3478 -a -o  //207.246.80.69是我的服务器外网地址</span><br></pre></td></tr></table></figure>

<ul>
<li>测试:</li>
</ul>
<p>打开 <a href="https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/" target="_blank" rel="noopener">https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/ </a>进行测试</p>
<p><img src="/p/697b4dfa/1.png" alt="image-20190129145807612"></p>
<p>测试时可以看log (<a href="https://github.com/coturn/coturn/wiki/README#logs" target="_blank" rel="noopener">https://github.com/coturn/coturn/wiki/README#logs</a>)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tail -f /var/tmp/turn_日期.log</span><br></pre></td></tr></table></figure>

<h2 id="2-搭建信令服务器simplemaster"><a href="#2-搭建信令服务器simplemaster" class="headerlink" title="2. 搭建信令服务器simplemaster"></a>2. 搭建信令服务器simplemaster</h2><p> 信令服务器使用的是 <a href="https://github.com/andyet/signalmaster" target="_blank" rel="noopener"><strong>signalmaster</strong></a> ，基于websocket。选用它的原因是可以直接集成turn server服务器。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">git clone https://github.com/andyet/signalmaster.git</span><br><span class="line">cd signalmaster</span><br><span class="line">npm install express</span><br><span class="line">npm install yetify</span><br><span class="line">npm install getconfig</span><br><span class="line">npm install node-uuid</span><br><span class="line">npm install socket.io</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">或者</span><br><span class="line"></span><br><span class="line">git clone https://github.com/nguyenphu160196/signalmaster.git</span><br><span class="line">cd signalmaster</span><br><span class="line">npm install</span><br></pre></td></tr></table></figure>

<p>signalmaster可以连接turnserver，但不支持用户名/密码方式，需要对源码sockets.js 110行进行调整，调整后的代码如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (!config.turnorigins || config.turnorigins.indexOf(origin) !== <span class="number">-1</span>) &#123;</span><br><span class="line">        config.turnservers.forEach(<span class="function"><span class="keyword">function</span> (<span class="params">server</span>) </span>&#123;</span><br><span class="line">            credentials.push(&#123;</span><br><span class="line">                username: server.username,</span><br><span class="line">                credential: server.credential,</span><br><span class="line">                urls: server.urls || server.url</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>完成后，修改config/production.json，配置turnserver的用户和密码，如下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"isDev"</span>: <span class="literal">true</span>,</span><br><span class="line">  <span class="attr">"server"</span>: &#123;</span><br><span class="line">    <span class="attr">"port"</span>: <span class="number">8888</span>,</span><br><span class="line">    <span class="attr">"/* secure */"</span>: <span class="string">"/* whether this connects via https */"</span>,</span><br><span class="line">    <span class="attr">"secure"</span>: <span class="literal">false</span>,</span><br><span class="line">    <span class="attr">"key"</span>: <span class="literal">null</span>,</span><br><span class="line">    <span class="attr">"cert"</span>: <span class="literal">null</span>,</span><br><span class="line">    <span class="attr">"password"</span>: <span class="literal">null</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">"rooms"</span>: &#123;</span><br><span class="line">    <span class="attr">"/* maxClients */"</span>: <span class="string">"/* maximum number of clients per room. 0 = no limit */"</span>,</span><br><span class="line">    <span class="attr">"maxClients"</span>: <span class="number">0</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">"stunservers"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"urls"</span>: <span class="string">"stun:stun.ekiga.net:3478"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">"turnservers"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"urls"</span>: [<span class="string">"turn:www.turn.cn:3478"</span>],</span><br><span class="line">      <span class="attr">"username"</span>: <span class="string">"user"</span>,</span><br><span class="line">      <span class="attr">"credential"</span>:<span class="string">"pass"</span>,  </span><br><span class="line">      <span class="attr">"expiry"</span>: <span class="number">86400</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>启动:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">nohup node server.js &amp;</span><br><span class="line"></span><br><span class="line">//Output:</span><br><span class="line">&amp;yet -- signal master is running at: http://localhost:8888</span><br></pre></td></tr></table></figure>

<h2 id="3-搭建Web客户端"><a href="#3-搭建Web客户端" class="headerlink" title="3. 搭建Web客户端"></a>3. 搭建Web客户端</h2><p>复制下面的代码，保存为一个html文件, 放在自己的服务器上</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://code.jquery.com/jquery-1.9.1.js"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"http://simplewebrtc.com/latest-v3.js"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="undefined"> </span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> webrtc = <span class="keyword">new</span> SimpleWebRTC(&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">// the id/element dom element that will hold "our" video</span></span></span><br><span class="line"><span class="javascript">            localVideoEl: <span class="string">'localVideo'</span>,</span></span><br><span class="line"><span class="javascript">            <span class="comment">// the id/element dom element that will hold remote videos</span></span></span><br><span class="line"><span class="javascript">            remoteVideosEl: <span class="string">'remoteVideos'</span>,</span></span><br><span class="line"><span class="javascript">            <span class="comment">// immediately ask for camera access</span></span></span><br><span class="line"><span class="javascript">            autoRequestMedia: <span class="literal">true</span>,</span></span><br><span class="line"><span class="javascript">            url:<span class="string">'http://207.246.80.69:8888'</span>,</span></span><br><span class="line"><span class="javascript">            nick:<span class="string">'nickname'</span></span></span><br><span class="line"><span class="undefined">        &#125;);</span></span><br><span class="line"><span class="undefined"> </span></span><br><span class="line"><span class="undefined"> </span></span><br><span class="line"><span class="undefined"> </span></span><br><span class="line"><span class="javascript">        <span class="comment">// we have to wait until it's ready</span></span></span><br><span class="line"><span class="javascript">        webrtc.on(<span class="string">'readyToCall'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">// you can name it anything</span></span></span><br><span class="line"><span class="javascript">            webrtc.joinRoom(<span class="string">'roomid'</span>);</span></span><br><span class="line"><span class="undefined"> </span></span><br><span class="line"><span class="javascript">            <span class="comment">// Send a chat message</span></span></span><br><span class="line"><span class="javascript">            $(<span class="string">'#send'</span>).click(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">var</span> msg = $(<span class="string">'#text'</span>).val();</span></span><br><span class="line"><span class="javascript">                webrtc.sendToAll(<span class="string">'chat'</span>, &#123; <span class="attr">message</span>: msg, <span class="attr">nick</span>: webrtc.config.nick &#125;);</span></span><br><span class="line"><span class="javascript">                $(<span class="string">'#messages'</span>).append(<span class="string">'&lt;br&gt;You:&lt;br&gt;'</span> + msg + <span class="string">'\n'</span>);</span></span><br><span class="line"><span class="javascript">                $(<span class="string">'#text'</span>).val(<span class="string">''</span>);</span></span><br><span class="line"><span class="undefined">            &#125;);</span></span><br><span class="line"><span class="undefined">        &#125;);</span></span><br><span class="line"><span class="undefined"> </span></span><br><span class="line"><span class="javascript">        <span class="comment">//For Text Chat ------------------------------------------------------------------</span></span></span><br><span class="line"><span class="javascript">        <span class="comment">// Await messages from others</span></span></span><br><span class="line"><span class="javascript">        webrtc.connection.on(<span class="string">'message'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">data</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">if</span> (data.type === <span class="string">'chat'</span>) &#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(<span class="string">'chat received'</span>, data);</span></span><br><span class="line"><span class="javascript">                $(<span class="string">'#messages'</span>).append(<span class="string">'&lt;br&gt;'</span> + data.payload.nick + <span class="string">':&lt;br&gt;'</span> + data.payload.message+ <span class="string">'\n'</span>);</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined">        &#125;);</span></span><br><span class="line"><span class="undefined">        </span></span><br><span class="line"><span class="undefined">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="undefined">        #remoteVideos video &#123;</span></span><br><span class="line"><span class="undefined">            height: 150px;</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="undefined"> </span></span><br><span class="line"><span class="undefined">        #localVideo &#123;</span></span><br><span class="line"><span class="undefined">            height: 150px;</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="undefined">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">textarea</span> <span class="attr">id</span>=<span class="string">"messages"</span> <span class="attr">rows</span>=<span class="string">"5"</span> <span class="attr">cols</span>=<span class="string">"20"</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span><span class="tag">&lt;<span class="name">br</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">id</span>=<span class="string">"text"</span> <span class="attr">type</span>=<span class="string">"text"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">id</span>=<span class="string">"send"</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"send"</span> /&gt;</span><span class="tag">&lt;<span class="name">br</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">video</span> <span class="attr">id</span>=<span class="string">"localVideo"</span>&gt;</span><span class="tag">&lt;/<span class="name">video</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"remoteVideos"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>nginx配置:</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">server</span> &#123;</span><br><span class="line">        <span class="attribute">listen</span> <span class="number">8080</span> default_server;</span><br><span class="line">        <span class="attribute">listen</span> [::]:<span class="number">8080</span> default_server;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">root</span> /home/liuwei/web;</span><br><span class="line">        <span class="attribute">index</span> index.html;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>打开firefox开始测试  <a href="http://207.246.80.69:8080" target="_blank" rel="noopener">http://207.246.80.69:8080</a>, chrome需要https</p>
</li>
<li><p><a href="http://simplewebrtc.com/latest-v3.js" target="_blank" rel="noopener">http://simplewebrtc.com/latest-v3.js</a> 可能丢失, 参考<a href="https://github.com/andyet/SimpleWebRTC/blob/gh-pages/latest-v3.js" target="_blank" rel="noopener">https://github.com/andyet/SimpleWebRTC/blob/gh-pages/latest-v3.js</a></p>
</li>
<li><p>其中url是信令服务器的地址、nickname、roomid根据需要修改</p>
</li>
</ul>
<h3 id="4-参考资料"><a href="#4-参考资料" class="headerlink" title="4. 参考资料:"></a>4. 参考资料:</h3><ul>
<li><p><a href="https://www.cnblogs.com/yubaolee/p/webrtc.html" target="_blank" rel="noopener">https://www.cnblogs.com/yubaolee/p/webrtc.html</a></p>
</li>
<li><p><a href="https://blog.csdn.net/csj6346/article/details/81455663" target="_blank" rel="noopener">https://blog.csdn.net/csj6346/article/details/81455663</a></p>
</li>
</ul>

    </div>

    
    
    
        
      
        <div id="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作, 一个耿直的笑脸~</div>
  <button id="reward-button" disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
        
      
      <div style="display: inline-block">
        <img src="/images/wechatpay.jpg" alt="Levon 微信支付">
        <p>微信支付</p>
      </div>
        
      
      <div style="display: inline-block">
        <img src="/images/alipay.jpg" alt="Levon 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/webrtc/" rel="tag"># webrtc</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/p/f3e0f5f5.html" rel="next" title="nat穿透,stun,turn,ice介绍">
                  <i class="fa fa-chevron-left"></i> nat穿透,stun,turn,ice介绍
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/p/6d00c950.html" rel="prev" title="javascript原形链的自我理解">
                  javascript原形链的自我理解 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    
  <div class="comments" id="comments">
    <div id="disqus_thread">
      <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
    </div>
  </div>
  
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-搭建NAT穿透服务器coturn"><span class="nav-number">1.</span> <span class="nav-text">1. 搭建NAT穿透服务器coturn</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-搭建信令服务器simplemaster"><span class="nav-number">2.</span> <span class="nav-text">2. 搭建信令服务器simplemaster</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-搭建Web客户端"><span class="nav-number">3.</span> <span class="nav-text">3. 搭建Web客户端</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-参考资料"><span class="nav-number">3.1.</span> <span class="nav-text">4. 参考资料:</span></a></li></ol></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/images/avatar.jpg"
      alt="Levon">
  <p class="site-author-name" itemprop="name">Levon</p>
  <div class="site-description" itemprop="description">Never Give Up</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        <span class="site-state-item-count">34</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">76</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/unix2dos" title="GitHub &rarr; https://github.com/unix2dos" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:levonfly@gmail.com" title="E-Mail &rarr; mailto:levonfly@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://twitter.com/levonfly" title="Twitter &rarr; https://twitter.com/levonfly" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://weibo.com/l6241425" title="Weibo &rarr; https://weibo.com/l6241425" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Levon</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">507k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:41</span>
</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>








  <script src="/js/local-search.js?v=7.4.0"></script>














  

  

  

<script>
  function loadCount() {
    var d = document, s = d.createElement('script');
    s.src = 'https://levonfly.disqus.com/count.js';
    s.id = 'dsq-count-scr';
    (d.head || d.body).appendChild(s);
  }
  // defer loading until the whole page loading is completed
  window.addEventListener('load', loadCount, false);
</script>
<script>
  function disqus_config() {
    this.page.url = "https://unix2dos.github.io/p/697b4dfa.html";
    this.page.identifier = "p/697b4dfa.html";
    this.page.title = '搭建webRTC视频聊天';};
  function loadComments() {
    if (window.DISQUS) {
      DISQUS.reset({
        reload: true,
        config: disqus_config
      });
    } else {
      var d = document, s = d.createElement('script');
      s.src = 'https://levonfly.disqus.com/embed.js';
      s.setAttribute('data-timestamp', '' + +new Date());
      (d.head || d.body).appendChild(s);
    }
  }
    window.addEventListener('load', loadComments, false);
  
</script>

</body>
</html>
